package sandbox.vaadin.jpa;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;

import com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider;

public abstract class MutableLocalEntityProviderAbstract<TYPE extends Serializable>
    extends MutableLocalEntityProvider<TYPE>
{
	public abstract EntityManager getEntityManager();

	protected MutableLocalEntityProviderAbstract(Class<TYPE> entityClass)
	{
		super(entityClass);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
  protected void runInTransaction(Runnable operation)
  {
    super.runInTransaction(operation);
  }
	
	@PostConstruct
	public void init()
	{
		setEntityManager(getEntityManager());

		// entityManager is transaction-scoped, which means that the entities will
		// be automatically detached when the transaction is closed. Therefore we
		// do not need to explicitly detach them.
		setEntitiesDetached(false);
	}
}